#include <tools.h>
#include <assert.h>
#include <standard/csv.h>
#include <standard/system.h>
#include "sceneobjectinfo.h"
#include "global.h"

CSceneObjectConfigManager::CSceneObjectConfigManager(){
    m_strResourceSpace = NULL;
}
CSceneObjectConfigManager::~CSceneObjectConfigManager(){
    _ClearSceneObjectCfgInfoList( &m_vSceneDecorationConfigList );
    _ClearSceneObjectCfgInfoList( &m_vSceneUnitConfigList );
}
void CSceneObjectConfigManager::Init( const char* strResourceSpace ){
    m_strResourceSpace = strResourceSpace;
    Reset();
}
bool32 CSceneObjectConfigManager::Reset(){
    asserts( ReloadDecorationConfig(), "" );
    asserts( ReloadUnitConfig(), "" );
    return True;
}
bool32 CSceneObjectConfigManager::ReloadDecorationConfig(){
    return LoadCfgFile( "sceneobject.csv", &m_vDecorationClass0List, &m_vDecorationClass1List, &m_vSceneDecorationConfigList );
}
bool32 CSceneObjectConfigManager::ReloadUnitConfig(){
    return  LoadCfgFile( "sceneunit.csv", &m_vUnitClass0List, &m_vUnitClass1List, &m_vSceneUnitConfigList );
}
bool32 CSceneObjectConfigManager::LoadCfgFile( const char* strFileName, vector<string32> *pClass0List, vector<string32> *pClass1List, list<SSceneObjectConfigInfo>* pSceneObjectList ){
    pClass0List->clear();
    pClass1List->clear();
    _ClearSceneObjectCfgInfoList( pSceneObjectList );


    char _strTextBuff[256];
    int32 _nSize = 0;
    char* _strTextContent = (char*)GetAssetFileData( Format( _strTextBuff, 256, "%s/gamespace/%s/%s", g_strResourceWorkPath, m_strResourceSpace, strFileName ), &_nSize, NULL );
    if( _strTextContent ){

        CCSV _sCsvLib;
        char* _strCode = _strTextContent;
        char* __str = _sCsvLib.Init( _strCode );
        if( __str ){
            __str = _sCsvLib.GetNextRow( __str );
            __str = _sCsvLib.GetNextRow( __str );
            __str = _sCsvLib.GetNextRow( __str );
            __str = _sCsvLib.GetNextRow( __str );
            __str = _sCsvLib.GetNextRow( __str );
        
            while( __str ){
                SSceneObjectConfigInfo _sSDCfgInfo;
                _sSDCfgInfo.m_nCfgID = _sCsvLib.GetRowItemInteger( 0 );
                strncpy( _sSDCfgInfo.m_strName, _sCsvLib.GetRowItem( 1 ), 32 );
                strncpy( _sSDCfgInfo.m_strFileName, _sCsvLib.GetRowItem( 2 ), 32 );
                _sSDCfgInfo.m_nClass0 = GetClassFormName( pClass0List, _sCsvLib.GetRowItem( 3 ) );
                _sSDCfgInfo.m_nClass1 = GetClassFormName( pClass1List, _sCsvLib.GetRowItem( 4 ) );
                _sSDCfgInfo.m_fScale = _sCsvLib.GetRowItemFloat( 5 );
                _sSDCfgInfo.m_eRotationType = (ESceneObjectRotationType)_sCsvLib.GetRowItemInteger( 6 );
                _sSDCfgInfo.m_nModelGroup = _sCsvLib.GetRowItemInteger( 7 );
                _sSDCfgInfo.m_fOperatorRadius = _sCsvLib.GetRowItemFloat( 8 );
                _sSDCfgInfo.m_eCellType = (ESceneObjectCellType)_sCsvLib.GetRowItemInteger( 9 );
                strncpy( _sSDCfgInfo.m_strDecelTexture, _sCsvLib.GetRowItem( 10 ), 32 );
                _sSDCfgInfo.m_fDecelSize = _sCsvLib.GetRowItemFloat( 11 );
                const char* _strData = _sCsvLib.GetRowItem( 12 );
                _sSDCfgInfo.m_strExtData = NewString( _strData );
                pSceneObjectList->push_back( _sSDCfgInfo );
                __str = _sCsvLib.GetNextRow( __str );
            }
        }
        _sCsvLib.Close();
        FREE( _strTextContent );
        return True;
    }
    return False;
}
int32 CSceneObjectConfigManager::GetClassFormName( vector<string32> *pClassList, const char* strName ){
    int32 i = 0;
    for( ; i < pClassList->size(); i ++ ){
        if( strcasecmp( strName, pClassList->at(i).c_str() ) == 0 ){
            return i;
        }
    }
    pClassList->push_back( strName );
    return i;
}
void CSceneObjectConfigManager::_ClearSceneObjectCfgInfoList( list<SSceneObjectConfigInfo>* pSceneObjectList ){
    for( list<SSceneObjectConfigInfo>::iterator it = pSceneObjectList->begin(); it != pSceneObjectList->end(); it ++ ){
        SSceneObjectConfigInfo* _pSceneObjectCfgInfo = &(*it);
        FREE( _pSceneObjectCfgInfo->m_strExtData );
    }
    pSceneObjectList->clear();
}
